package com.sjm.bookshop.utils;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Verification;
import com.sjm.bookshop.entity.Admin;
import com.sjm.bookshop.entity.User;

import java.util.Calendar;

public class JwtUtils {
    private static final String KEY = "sjmSjm";

    /**
     * 生成token
     *
     * @param obj
     * @return
     */
    public static String createToken(Object obj) {
//        设置过期时间,1天过期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
//        claim :声明
        JWTCreator.Builder builder = JWT.create()
//                payload的内容，由一个个的claim组成
                .withClaim("userInfo", JSON.toJSONString(obj))
                .withExpiresAt(calendar.getTime());
        String token = builder.sign(Algorithm.HMAC256(KEY));
        return token;
    }

    /**
     * 生成token
     *
     * @param admin
     * @return
     */
//    public static String createToken(Admin admin) {
////        设置过期时间,1天过期
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_YEAR, 1);
////        claim :声明
//        JWTCreator.Builder builder = JWT.create()
////                payload的内容，由一个个的claim组成
//                .withClaim("adminInfo", JSON.toJSONString(admin))
//                .withExpiresAt(calendar.getTime());
//        String token = builder.sign(Algorithm.HMAC256(KEY));
//        return token;
//    }


    /**
     * token校验
     *
     * @param token
     * @return
     */
    public static boolean verify(String token) {
        try {
            Verification require = JWT.require(Algorithm.HMAC256(KEY));
            DecodedJWT verify = require.build().verify(token);
            System.out.println("token--->"+token);
            if (verify != null){
                return true;
            }

        } catch (Exception e){
            e.printStackTrace();
            System.out.println("token认证失败，需要重新登录！");
        }
//        catch (SignatureVerificationException e) {
//            e.printStackTrace();
//            System.out.println("签名不一致");
//        } catch (TokenExpiredException e) {
//            e.printStackTrace();
//            System.out.println("令牌过期");
//        } catch (AlgorithmMismatchException e) {
//            e.printStackTrace();
//            System.out.println("签名算法不一致");
//        } catch (InvalidClaimException e) {
//            e.printStackTrace();
//            System.out.println("payload不可用");
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("校验失败");
//        }
        return false;
    }

    /**
     * token解析为User对象
     *
     * @return
     */
    public static User tokenParseUser(DecodedJWT decodedJWT) {
        Claim claim = decodedJWT.getClaim("userInfo");
        if (claim != null) {
            String userString = claim.asString();
            User user = JSON.parseObject(userString, User.class);
            return user;
        } else {
            return null;
        }
    }


    /**
     * token解析为User对象
     *
     * @return
     */
    public static Admin tokenParseAdmin(DecodedJWT decodedJWT) {
        Claim claim = decodedJWT.getClaim("userInfo");
        if (claim != null) {
            String adminString = claim.asString();
            Admin admin = JSON.parseObject(adminString, Admin.class);
            return admin;
        } else {
            return null;
        }
    }
}
